package xyz.penghaonan.wificonnect.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import xyz.penghaonan.wificonnect.WifiConnectSDK;
import xyz.penghaonan.wificonnect.connect.IConnectHelper;

public class MainService extends Service {

    private final IConnectHelper.OnConnectStateChangedListener onConnectStateChangedListener = new IConnectHelper.OnConnectStateChangedListener() {
        @Override
        public void onStateChanged(int state) {
            updateNotification();
        }
    };

    public MainService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        updateNotification();
        WifiConnectSDK.getConfig().getConnectHelper().addOnDeviceConnectStateListener(onConnectStateChangedListener);
        return new ServiceBinder(this);
    }

    public void updateNotification() {
        if (WifiConnectSDK.getConfig() != null) {
            startForeground(WifiConnectSDK.getConfig().getServiceNotificationId(), WifiConnectSDK.getConfig().getServiceNotification());
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        stopForeground(true);
        WifiConnectSDK.getConfig().getConnectHelper().removeOnDeviceConnectStateListener(onConnectStateChangedListener);
        return super.onUnbind(intent);
    }
}